<?php
    session_start();
	if ($_SESSION['login'] == true)
	{
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>setting</title>
<link rel=stylesheet href="css/setting.css" type="text/css">
<script type="text/javascript" src="js/mootools-1.3.2-core.js"></script>
<script type="text/javascript" src="js/mootools-1.3.2.1-more.js"></script>
<script type="text/javascript" src="js/jquery-1.7.min.js"></script>
<script>
    var $j = jQuery.noConflict();
</script>
<script type="text/javascript" src="js/utility.js"></script>
<script type="text/javascript" src="js/setting.js"></script>
</head>
<body>
<div id="top">
    <div id="right_bar">
        <div id="backFrontPage" style='cursor:pointer;' onmouseover="loginover(this)" onmouseout="loginout(this)" onclick="frontpageClick()" onmousedown="logindown(this)" onmouseup="loginup(this)"><span class='navClass'>返回首页</span></div>
        <div id="logout" unselectable="on" class="unselectable" style='cursor:pointer;'onmouseover="logoutover(this)" onmouseout="logoutout(this)" onclick="logoutclick()" onmousedown="logoutdown(this)" onmouseup="logoutup(this)"><span class='navClass'>登出<span></div>
    </div>
</div>
<div id='favLists'>
<?php
    require('config.php');
    $stmt = $connection->prepare("select fav_type, create_time from user_fav_lists where user_id=? order by create_time");
	$stmt->bind_param("s", $_COOKIE['userid']);
	$stmt->execute();
    $stmt->bind_result($fav_type, $create_time);
    $fav_types = array();
    $create_times = array();
    while ($stmt->fetch()) 
    {
        array_push($fav_types, $fav_type);
        array_push($create_times, $create_time);
        //echo $fav_type . '    ' . $create_time;
    }
    $stmt->close();
    $fav_numbers = array();
    $begin = true;
    foreach ($fav_types as $fav_type)
    {
        if ($begin)
        {
            $stmt = $connection->prepare("select count(*) from user_fav where user_id=? limit 1");
    		$stmt->bind_param("s", $_COOKIE['userid']);
            $begin = false;
        }
        else
        {
            $stmt = $connection->prepare("select count(*) from user_fav where user_id=? and fav_type=?");
    		$stmt->bind_param("ss", $_COOKIE['userid'], $fav_type);
        }
    	$stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        //echo $fav_type . '    ' . $count . '\\';
        array_push($fav_numbers, $count);
        $stmt->close();
    }
    for ($i = 0; $i<count($fav_types); ++$i)
    {
?>
    <div id='favList<?php echo $i?>' class='favList'>
<?php
        if ($i == 0)
        {
?>
        <div class='favList_left'>
        <a href='index.php?fav_type=<?php echo $fav_types[$i]; ?>'><span id='fav_type<?php echo $i;?>'><?php echo $fav_types[$i]; ?></span></a> ( <?php echo $fav_numbers[$i] ?> )
        </div>
<?php
        }
        else
        {
?>
        <div class='favList_left'>
            <a href='index.php?fav_type=<?php echo $fav_types[$i]; ?>'><span id='fav_type<?php echo $i;?>'><?php echo $fav_types[$i]; ?></span></a> ( <?php echo $fav_numbers[$i] ?> ) 创建时间：  <?php echo $create_times[$i] ?>
        </div>
        <div id='edit<?php echo $i;?>' class='edit' onclick='renameClick(this)'>重命名</div>
        <div id='delete<?php echo $i;?>' class='delete' onclick='deleteClick(this)'>删除</div>
<?php
        }
?>
    </div>
<?php
    }
?>
</div>
<div id='createNew'><div id='createNewFavList' onclick='createList()'>新建列表</div></div>
<p id='testmessage'></p>
</body>
</html>
<?php
	}
    else if (isset($_COOKIE['userid']) && isset($_COOKIE['usermd5']))
	{
	    require('config.php');
		$stmt = $connection->prepare("select * from user_id_passwd where id=? and md5=? and cookie=1");
		$stmt->bind_param("ss", $_COOKIE['userid'], $_COOKIE['usermd5']);
		$stmt->execute();
		$stmt->store_result();
		$row_cnt = $stmt->num_rows;
		if ($row_cnt > 0)
        {
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>setting</title>
<link rel=stylesheet href="css/setting.css" type="text/css">
<script type="text/javascript" src="js/mootools-1.3.2-core.js"></script>
<script type="text/javascript" src="js/mootools-1.3.2.1-more.js"></script>
<script type="text/javascript" src="js/jquery-1.7.min.js"></script>
<script>
    var $j = jQuery.noConflict();
</script>
<script type="text/javascript" src="js/setting.js"></script>
</head>
<body>
<div id="top">
    <div id="right_bar">
        <div id="backFrontPage" style='cursor:pointer;' onmouseover="loginover(this)" onmouseout="loginout(this)" onclick="frontpageClick()" onmousedown="logindown(this)" onmouseup="loginup(this)"><span class='navClass'>返回首页</span></div>
        <div id="logout" unselectable="on" class="unselectable" style='cursor:pointer;'onmouseover="logoutover(this)" onmouseout="logoutout(this)" onclick="logoutclick()" onmousedown="logoutdown(this)" onmouseup="logoutup(this)"><span class='navClass'>登出<span></div>
    </div>
</div>
<div id='favLists'>
<?php
    require('config.php');
    $stmt = $connection->prepare("select fav_type, create_time from user_fav_lists where user_id=? order by create_time");
	$stmt->bind_param("s", $_COOKIE['userid']);
	$stmt->execute();
    $stmt->bind_result($fav_type, $create_time);
    $fav_types = array();
    $create_times = array();
    while ($stmt->fetch()) 
    {
        array_push($fav_types, $fav_type);
        array_push($create_times, $create_time);
        //echo $fav_type . '    ' . $create_time;
    }
    $stmt->close();
    $fav_numbers = array();
    $begin = true;
    foreach ($fav_types as $fav_type)
    {
        if ($begin)
        {
            $stmt = $connection->prepare("select count(*) from user_fav where user_id=? limit 1");
    		$stmt->bind_param("s", $_COOKIE['userid']);
            $begin = false;
        }
        else
        {
            $stmt = $connection->prepare("select count(*) from user_fav where user_id=? and fav_type=?");
    		$stmt->bind_param("ss", $_COOKIE['userid'], $fav_type);
        }
    	$stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        //echo $fav_type . '    ' . $count . '\\';
        array_push($fav_numbers, $count);
        $stmt->close();
    }
    for ($i = 0; $i<count($fav_types); ++$i)
    {
?>
    <div id='favList<?php echo $i?>' class='favList'>
<?php
        if ($i == 0)
        {
?>
        <div class='favList_left'>
        <a href='index.php?fav_type=<?php echo $fav_types[$i]; ?>'><span id='fav_type<?php echo $i;?>'><?php echo $fav_types[$i]; ?></span></a> ( <?php echo $fav_numbers[$i] ?> )
        </div>
<?php
        }
        else
        {
?>
        <div class='favList_left'>
            <a href='index.php?fav_type=<?php echo $fav_types[$i]; ?>'><span id='fav_type<?php echo $i;?>'><?php echo $fav_types[$i]; ?></span></a> ( <?php echo $fav_numbers[$i] ?> ) 创建时间：  <?php echo $create_times[$i] ?>
        </div>
        <div id='edit<?php echo $i;?>' class='edit' onclick='renameClick(this)'>重命名</div>
        <div id='delete<?php echo $i;?>' class='delete' onclick='deleteClick(this)'>删除</div>
<?php
        }
?>
    </div>
<?php
    }
?>
</div>
<p id='testmessage'></p>
</body>
</html>
<?php
		}
		else
		{
            $_SESSION['gate'] = 1;
		    header("location: anonymous.php");
		}
	}
    else
    {
        $_SESSION['gate'] = 1;
	    header("location: anonymous.php");
    }
?>
